<template>
	<view class="root">
		
		
		
		<view class="header">
			<text>国家励志奖学金申请</text>
		</view>

		<view class="">
			
			<u-form :model="form" ref="uForm">
				<u-tag text="个人信息填写" shape="circleRight" />
				<u-form-item label="姓名" label-position="top" prop="name">
					<u-input v-model="form.name" />
				</u-form-item>
				
				<u-form-item label="学号" label-position="top" prop="stu_id">
					<u-input v-model="form.stu_id" />
				</u-form-item>
				
				<u-form-item label="身份证号" label-position="top" prop="id">
					<u-input v-model="form.id" />
				</u-form-item>
			</u-form>
			
			<u-button @click="login">进入并保存</u-button>
			
		</view>
		
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					stu_id: '',
					id:'',
					name:'',
					xueyuan: '智能制造学院',
					name: '',
					major: '',
					sex: '',
					nation: '',
					schoolYear: '',
					stu_id: '',
					cla: '',
					tel: '',
					id: '',
					kunnan_level: '',
					classNum: '',
					classRank: '',
					comEval: '',
					requiredCourseNum:'',
					requiredCourseNumPass:'',
					awardinfo: [],
					applyReason: ''
				},
				stu_idEm:'',
				idEm:'',
				nameEm:'',
				template:'请输入',
				rules:{
					name: [{
						required: true,
						message: '请输入姓名',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}],
					stu_id: [{
						required: true,
						message: '请输入学号',
						trigger: ['change', 'blur'],
					},{
						min: 10,
						message: '学号长度为10位',
						trigger: 'change'
					},{
						max: 10,
						message: '学号长度为10位',
						trigger: 'change'
					}],
					id: [{
						required: true,
						message: '请输入身份证号',
						trigger: ['change', 'blur'],
					},{
						min: 18,
						message: '身份证号为18位',
						trigger: 'change'
					},{
						max: 18,
						message: '身份证号为18位',
						trigger: 'change'
					}],
				}
			}
		},
		methods: {
			login(){
				this.$refs.uForm.validate(valid => {
					if(valid){
						let db = uniCloud.database()
						uni.showLoading({
							title:'正在进入',
						})
						db.collection('guoli').where({
							stu_id:this.form.stu_id
						}).count().then(res =>{
							if(res.result.total == 0){
								db.collection('guoli').add(this.form).then(res =>{
									uni.setStorage({
										key:'stu_id',
										data:this.form.stu_id
									})
									uni.hideLoading()
									this.$refs.uToast.show({
										title:'保存成功',
										type:'success',
										url:'/pages/guoli/guoli'
									})
								})
							}else if(res.result.total == 1){
								db.collection('guoli').where({
									stu_id:this.form.stu_id
								}).update({
									name:this.form.name,
									stu_id:this.form.stu_id,
									id:this.form.id
								}).then(()=>{
									uni.setStorage({
										key:'stu_id',
										data:this.form.stu_id
									})
									uni.hideLoading()
									// url填绝对路径
									this.$refs.uToast.show({
										title:'保存成功',
										type:'success',
										url:'/pages/guoli/index/index'
									})
									
								})
								
							}
						})
					}else{
						console.log('验证失败');
					}
				});
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style>
	.root {
		
		
		padding: 20px;
		background-color: #ffffff;
		
	}
	.header {
		
		display: flex;
		justify-content: center;
		margin: 0 0 10px;
	}
	
	.header text {
		font-size: 18px
	}
</style>
